﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Alex.DataLibrary.Enums;
using Alex.DataLibrary.Tables;
using Microsoft.Win32;

namespace Alex.ShipDataSaver
{
    public static class TablesCollection
    {    	
        public static void AddTable(string nameTable, TableType tableType, ArrayList columnList)
        {
            switch (tableType)
            {
                case TableType.Standart:
            		tables.Add(nameTable, new DataTableStandart(nameTable));
                    foreach (object col in columnList) tables[nameTable].Table.Columns.Add(col.ToString());
                    break;
                case TableType.Compare:
                    tables.Add(nameTable, new DataTableDifference(nameTable));
                    break;
            }
        }

        public static void DeleteTable(string nameTable)
        {
            tables.Remove(nameTable);
        }

        public static DataTable GetTable(string nameTable)
        {
            return tables[nameTable].Table;
        }
        
        public static void ClearTables()
        {
        	tables.Clear();
        }

        public static ArrayList OpenTables()
        {
            ArrayList items = new ArrayList();

            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.Filter = "Xml files (*.xml)|*.xml";

            fileDialog.ShowDialog();

            if (fileDialog.FileName != "")
            {
                TextReader textReader = new StreamReader(fileDialog.FileName);

                XmlSerializer sr = new XmlSerializer(typeof(TablesDictionary));

                ClearTables();
                tables = (TablesDictionary)sr.Deserialize(textReader);

                foreach (string name in tables.Keys)
                {
                    tables[name].Initialize();
                    items.Add(name);
                }         	            	
            }

            return items;
        }

        public static void SaveTables()
        {
            if (filePatch == "")
            {
                SaveFileDialog fileDialog = new SaveFileDialog();

                fileDialog.Filter = "Xml files (*.xml)|*.xml";

                fileDialog.ShowDialog();

                filePatch = fileDialog.FileName;
            }

            if (filePatch != "")
            {
                TextWriter textWriter = new StreamWriter(filePatch);

                XmlSerializer sr = new XmlSerializer(typeof(TablesDictionary));
                
                sr.Serialize(textWriter, tables);
            	textWriter.Close();
            }
        }
                
        public static ArrayList ColumnList { private get; set; }

        private static TablesDictionary tables = new TablesDictionary();

        private static string filePatch = "";
    }
}
